package com.anuo.app.modules.coach.controller;

import com.anuo.app.common.globalhandle.BaseController;
import com.anuo.app.common.basedata.AuditState;
import com.anuo.app.common.entity.Request;
import com.anuo.app.common.utils.JsonUtil;
import com.anuo.app.modules.coach.entity.Coach;
import com.anuo.app.modules.coach.entity.CoachOrganization;
import com.anuo.app.modules.coach.service.SystemManageService;
import com.fasterxml.jackson.core.type.TypeReference;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RestController;

@RestController
public class SystemManageController extends BaseController {
    @Autowired
    SystemManageService systemManageService;

    @PostMapping(value = "v1/apiSaveCoachAudit", produces = {"application/json;charset=UTF-8"})
    public String apiSaveCoachAudit(@RequestBody String encryptedReq) {
        //反序列化取得对象
        Coach coach = JsonUtil.fromJson(encryptedReq,
                new TypeReference<Request<Coach>>() {
                }).getFirstEntity();
        if (AuditState.REFUSE.equals(coach.getState())) {
            systemManageService.saveCoachAuditRefuse(coach);
        } else if (AuditState.PASS.equals(coach.getState())) {
            systemManageService.saveCoachAuditPass(coach);
        }
        return rspSuccess();
    }

    /**
     * 添加,修改组织机构
     *
     * @param encryptedReq
     * @return
     */
    @PostMapping(value = "v1/apiSaveOrganization", produces = {"application/json;charset=UTF-8"})
    public String apiSaveOrganization(@RequestBody String encryptedReq) {
        //反序列化取得对象
        CoachOrganization coachOrganization = JsonUtil.fromJson(encryptedReq,
                new TypeReference<Request<CoachOrganization>>() {
                }).getFirstEntity();
        boolean isExistOrganizationName = systemManageService.isExistOrganizationName(coachOrganization);
        if (isExistOrganizationName) {
            return rspError("已存在此名称的机构, 请换个新名称再添加");
        }

        systemManageService.saveCoachOrganization(coachOrganization);

        return rspSuccess();
    }
}

